baselib.report(0x00, "TD4.5.6 beginning.")
-- 下面是初始化操作:
busilib.SetOrientation(1)             -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
-- 第一步:CVS transitions to Unattached.SRC
busilib.setUsbCcs(CCS.Unattached) 
busilib.setUsbRole(ROLE.Source)   
-- 第二步:CVS waits 5ms
baselib.delay(5)
-- 第三步:CVS verifies PUT:
-- 3.a步:Presents Rd within max dcSRC.DRP x max tDRP [TD.4.5.6.V.1]
baselib.delay(70)                   -- 等待max dcSRC.DRP(0.7) x max tDRP(100)
local VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mV.")
if VCC1 < 200 then
    baselib.report(0x00, "TD4.5.6.V.1 pass.")
else
    baselib.report(0x00, "TD4.5.6.V.1 fail.")
end
-- 3.b步:Transitions from Rp to Rd within tDRPTransition [TD.4.5.6.V.2]
-- 不理解这个步骤
-- 3.c步:Applies Rd for tCCDebounce [TD.4.5.6.V.3]
--[[从T1+100ms 开始,开始连续N次(暂定N=5,间隔1ms)检测CC1电压处于0.25v - 1.50v之间的,并设置超时时间为100ms :
(1)若检测到连续5次满足,上报TD.4.5.6.V3为PASS
(2)若在100ms都不能连续5次满足,上报TD.4.5.6.V3为FAIL,同时上报最后一次检测到的CC1电压]]
local waitResult = busilib.waitCondition4ad(ADC.CC1, 4, 200, 1600, 100, 200)
local Tfour = baselib.get_time()
local Tpresent = math.floor((Tfour - Tone) / 1000)
if waitResult == LuaOk then
    baselib.report(0x00, "Rd is presented for at least 100ms within " .. Tpresent .. "ms.")
    baselib.report(0x00, "TD4.5.6.V.3 pass.")
else
    baselib.report(0x00, "Rd presentation timeout.")
    baselib.report(0x00, "TD4.5.6.V.3 fail.")
end
-- 第四步:CVS transitions to Try.SNK
-- 目前没有Try.SNK状态
-- 第五步:CVS verifies PUT attaches Rp within tPDDebounce min and tPDDebounce max + tDRPTransition [TD.4.5.6.V.4]
-- CVS验证PUT在tPDDebounce最小值和tPDDebounce最大值 + tDRPTransition时间内连接Rp
baselib.delay(21)                     -- 等待tPDDebounce最大值(20) + tDRPTransition(1)
VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mV.")
if VCC1 > 200 then
    baselib.report(0x00, "TD4.5.6.V.4 pass.")
else
    baselib.report(0x00, "TD4.5.6.V.4 fail.")
end
-- 第六步:CVS verifies that PUT transitions to Attached.SRC after tCCDebounce:
-- 6.a步:PUT source Vbus with tVbusON. [TD.4.5.6.V.5]
baselib.delay(275)               -- tVBUSON:275ms
if cstmlib.VBusVerified(1) then 
    baselib.report(0x00, "TD4.5.6.V.5 pass.")
else
    baselib.report(0x00, "TD4.5.6.V.5 fail.")
end
-- 6.b步:If PUT supports PD:PUT and CVS negotiate an explicit contract. [TD.4.5.6.V.6]
if USB_PD_Support then
    baselib.report(0x00, "Begin power negotiation.")
    tcpclib.SetPermission(1, 2)
    PN_result = tcpclib.PresetPowerNegotiation(1, 500)  --timeout is 500ms
    if PN_result == LuaOk then
        baselib.report(0x00, "TD4.5.6.V.6 pass.")
    else
        if PN_result == LuaTimeout then
            baselib.report(0x00, "Power negotiation timeout.")
        else
            baselib.report(0x00, "Unsupported power request received.")
        end
        baselib.report(0x00, "TD4.5.6.V.6 fail.")
    end
end
-- 6.d步:Else if PUT supports USB 2.0 DFP:PUT starts data communications on its D+/D- pair. [TD.4.5.6.V.8]
-- 不会
-- 第7步:CVS transitions to Disabled 
busilib.setUsbRole(ROLE.Disabled)
-- 第8步:CVS verifies that PUT transitions to Unattached.SNK within tSRCDisconnect:
-- 8.a步:PUT stops sourcing Vbus within tVbusOFF [TD.4.5.6.V.9]
baselib.delay(650)      
VBus = busilib.GetADC(ADC.VBUS, 4)
baselib.report(0x00, "VBus is " .. VBus .. "mV.")
if VBus < 3670 then 
    baselib.report(0x00, "TD4.5.6.V.9 pass.")
else
    baselib.report(0x00, "TD4.5.6.V.9 fail.")
end
-- 8.b步:PUT data communication has ceased. [TD.4.5.6.V.10]